package com.central.wx.mq

import cn.binarywang.wx.miniapp.api.WxMaService
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage
import cn.hutool.core.bean.BeanUtil
import com.central.common.model.*
import com.central.common.utils.formatStandard
import com.central.wx.config.WxMaConfiguration
import com.central.wx.config.WxMaProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.cloud.stream.annotation.StreamListener
import org.springframework.messaging.handler.annotation.Payload
import org.springframework.stereotype.Service
import java.util.*


const val REWARD_TEMPLATE_ID = "e7LYRsFpTp1b9F5QlAKVlTcyz1QR92taRHp8DG6RZiQ"
const val COMMENT_REPLY_TEMPLATE_ID = "MaUUvn5QnM3XY1YvPi4pEwVK3oEQngClV9RthRTyI3I"
const val NEW_GOOD_TEMPLATE_ID = "4GyaLhFXblxbtKYYlVmzLEkSbdTet3V9E4P7MlV-oRM"

/**
 * @author  fly
 * @date  2021/6/2 15:03
 * @version 1.0
 */
@Service
@EnableConfigurationProperties(WxMaProperties::class)
open class SubscribeService(wxMaProperties: WxMaProperties) {

    private var wxMaService: WxMaService = WxMaConfiguration.getMaService(wxMaProperties.configs[0].appid)


    /**
     * 推送发布帖子奖励订阅消息
     */
    @StreamListener(REWARD_INPUT)
    fun pushRewardMessage(@Payload postPushParam: PostPushParam) = wxMaService.subscribeService.sendSubscribeMsg(WxMaSubscribeMessage().apply {
        toUser = postPushParam.openId
        templateId = REWARD_TEMPLATE_ID
        data = BeanUtil.beanToMap(RewardParam().apply {
            amount1 = "5积分"
            date2 = Date().formatStandard()
            thing3 = "你分享的 ${postPushParam.content} 帖子获得了5积分"
            thing4 = "分享帖子获得积分"
            thing5 = "点击查看详情"
        }).map { (k, v) ->
            val msgData = WxMaSubscribeMessage.MsgData()
            msgData.name = k
            msgData.value = v.toString()
            msgData
        }.toList()
        page = "/page/${postPushParam.id}"
    })

    /**
     * 推送评论回复消息订阅
     */
    @StreamListener(COMMENT_INPUT)
    fun pushCommentReplyMessage(@Payload commentReplyPushParam: CommentReplyPushParam) = wxMaService.subscribeService.sendSubscribeMsg(WxMaSubscribeMessage().apply {
        toUser = commentReplyPushParam.openId
        templateId = COMMENT_REPLY_TEMPLATE_ID
        data = BeanUtil.beanToMap(CommentReplyParam().apply {
            name1 = commentReplyPushParam.userName
            time2 = commentReplyPushParam.replyTime
            thing3 = commentReplyPushParam.content
            thing4 = commentReplyPushParam.originalContent
        }).map { (k, v) ->
            val msgData = WxMaSubscribeMessage.MsgData()
            msgData.name = k
            msgData.value = v.toString()
            msgData
        }.toList()
        page = when (commentReplyPushParam.commentType) {
            1 -> "/page/post/${commentReplyPushParam.relevanceId}"
            2 -> "/page/newGood/${commentReplyPushParam.relevanceId}"
            else -> "/page/authenticate/${commentReplyPushParam.relevanceId}"
        }
    })

    @StreamListener(NEW_GOOD_INPUT)
    fun pushNewGoodMessage(@Payload newGoodPushParam: NewGoodPushParam) {

        newGoodPushParam.openId?.forEach {

            wxMaService.subscribeService.sendSubscribeMsg(WxMaSubscribeMessage().apply {
                toUser = it
                templateId = NEW_GOOD_TEMPLATE_ID
                data = BeanUtil.beanToMap(NewGoodParam().apply {
                    thing1 = newGoodPushParam.goodName
                    thing2 = newGoodPushParam.goodDetail
                    date3 = newGoodPushParam.goodTime
                    amount5 = newGoodPushParam.goodPrice
                }).map { (k, v) ->
                    val msgData = WxMaSubscribeMessage.MsgData()
                    msgData.name = k
                    msgData.value = v.toString()
                    msgData
                }.toList()
                page = "/page/newGood/${newGoodPushParam.goodId}"
            })
        }
    }
}